package com.example.tests;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class ContactDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exists, please remote it manually: " + file);
			return;
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else { 
			System.out.println("Unknown format " + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) {
		
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName() + "," + contact.getLastName() + "," 
					+ contact.getFirstAddress() + "," + contact.getFirstHome() + "," 
					+ contact.getMobile() + "," + contact.getWork() + "," 
					+ contact.getFirstEmail() + "," + contact.getSecondEmail() + "," 
					+ contact.getSecondAddress() + "," + contact.getSecondHome() + "," 
					+ contact.getBirthDay() + "," + contact.getMonth() + "," 
					+ contact.getYear() + "\n");
		}
		writer.close();
	}

	public static List<ContactData> generateRandomContacts(int amount) {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			ContactData contact = new ContactData()
			.withFirstName(generateRandomString())
			.withLastName(generateRandomString())
			.withFirstAddress(generateRandomString())
			.withFirstHome(generateRandomString())
			.withMobile(generateRandomString())
			.withWork(generateRandomString())
			.withFirstEmail(generateRandomString())
			.withSecondEmail(generateRandomString())
			.withSecondAddress(generateRandomString())
			.withSecondHome(generateRandomString())
			//.withBirthDay = "-"
			//.withMonth = "-"
			.withYear(generateRandomString());
			// contact.group
			list.add(contact);
		}
			return list;
	}
		
		public static String generateRandomString() {
			Random rnd = new Random();
			if (rnd.nextInt(3) == 0) {
				return "";
			} 	else {
				return "test" + rnd.nextInt();
			}
		}
	}	
